/**
 * 
 */
package com.java.test.user.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.java.test.datacenter.dto.SumAccountInfo;
import com.java.test.user.dto.AcountReset;

/**
 * @author 账号充值
 *
 */
@Component
public class AcountResetMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int insertacountReset(AcountReset info) {
		String sql = "insert into member_account_reset" + ""
				+ "(account_id,reset_type,chang_amount,multiple,remarks)values(?,?,?,?,?)";
		int i = jdbcTemplate.update(sql, info.getAccountId(), info.getResetType(), info.getChangAmount(),
				info.getMultiple(), info.getRemarks());
		return i;
	}

	// 手動存款
	public SumAccountInfo selecthandaddStatisticsData(LocalDateTime startTime, LocalDateTime endTime) {

		String sql = "select count(*) from member_account_reset where reset_type=1 ";

		String sqlitem = "";
		if (startTime == null && endTime == null) {
			sqlitem += " and TO_DAYS(create_time) = TO_DAYS(NOW())";
		} else {
			if (startTime != null)
				sqlitem += " and create_time >='" + startTime + "'";

			if (endTime != null)
				sqlitem += " and create_time <='" + endTime + "'";
		}
		int count = jdbcTemplate.queryForObject(sql + sqlitem, Integer.class);

		String sql2 = sqlitem + " group by account_id";
		List<Integer> list = jdbcTemplate.query(sql + sql2, new RowMapper<Integer>() {
			@Override
			public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getInt(1);
			}
		});

		String sqlamount = "select sum(chang_amount) from member_account_reset where reset_type=1 ";
		Double totalAmount = jdbcTemplate.queryForObject(sqlamount + sqlitem, Double.class);

		SumAccountInfo info = new SumAccountInfo("", count,
				(list == null || list.size() <= 0) == true ? 0 : list.size(), totalAmount == null ? 0 : totalAmount);
		return info;
	}

	// 手動扣款
	public SumAccountInfo selecthandreduceStatisticsData(LocalDateTime startTime, LocalDateTime endTime) {

		String sql = "select count(*) from member_account_reset where reset_type=0 ";

		String sqlitem = "";
		if (startTime == null && endTime == null) {
			sqlitem += " and TO_DAYS(create_time) = TO_DAYS(NOW())";
		} else {
			if (startTime != null)
				sqlitem += " and create_time >='" + startTime + "'";

			if (endTime != null)
				sqlitem += " and create_time <='" + endTime + "'";
		}
		int count = jdbcTemplate.queryForObject(sql + sqlitem, Integer.class);

		String sql2 = sqlitem + " group by account_id";
		List<Integer> list = jdbcTemplate.query(sql + sql2, new RowMapper<Integer>() {
			@Override
			public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getInt(1);
			}
		});

		String sqlamount = "select sum(chang_amount) from member_account_reset where reset_type=0 ";
		Double totalAmount = jdbcTemplate.queryForObject(sqlamount + sqlitem, Double.class);

		SumAccountInfo info = new SumAccountInfo("", count,
				(list == null || list.size() <= 0) == true ? 0 : list.size(), totalAmount == null ? 0 : totalAmount);
		return info;
	}

}
